<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Option;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Dcat\Admin\Widgets\Tab;

class OptionController extends AdminController
{
    public function __construct(Request $request)
    {
        $this->type = explode('/', $request->path())[2];
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Option(), function (Grid $grid) {
            $grid->header(function ($collection) {
                return '<script>$(".text-capitalize").html("' . (new Option())->get_types($this->type) . '设置")</script>';
            });
            if (in_array($this->type, ['college', 'city'])) {
                $grid->selector(function (Grid\Tools\Selector $selector) {
                    $selector->selectOne('remark', '选择省', (new Option())->get_province());
                });
            }

            $grid->model()->where('type', $this->type);
            $grid->sort->orderable();
            $grid->model()->orderBy('sort');
            $grid->column('option');
            $grid->column('sort');
            if ($this->type == "college") {
                $grid->column('map_ids', '标签')->checkbox((new Option())->get_college_tab(), true);
            }
            $grid->column('updated_at')->width('25%');
            $grid->disableViewButton();
            $grid->disableFilterButton();
//            $grid->export();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Option(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('option');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Option(), function (Form $form) {
            $form->display('id');
            $form->select('type')->options((new Option())->get_types())->value($this->type)->readOnly(); //TODO
            $form->text('option');
            if (in_array($this->type, ['college', 'city'])) {
                $form->select('remark', '省份')->options((new Option())->get_province());
            }
            if ($this->type == "college") {
                $form->checkbox('map_ids', '标签')->options((new Option())->get_college_tab())->saveAsJson();
            }
            $form->number('sort');
            $form->saved(function (Form $form, $result) {
                (new \App\Api\Repositories\OptionRepositories())->delete_cache($form->model()->type);
            });
            if ($form->isDeleting()) {
                (new \App\Api\Repositories\OptionRepositories())->delete_cache($form->model()->type);
            }
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
